package org.nuonuo.sdk.client;

import org.nuonuo.sdk.exception.NException;
import org.nuonuo.sdk.model.base.NResponse;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Version 1.0
 * @Author az
 * @Date 2024-04-01 14:00:07
 * @ClassName NSDK
 * @Description
 */
public class NSDK {

    private static final NSDK INSTANCE = new NSDK();

    public static final NSDK getIntance() {
        return INSTANCE;
    }

    private NSDK() {
    }

    public String getMerchantToken(String appKey, String appSecret, String... url) {
        this.verify(appKey, "AppKey不能为空");
        this.verify(appSecret, "AppSecret不能为空");
        return ModuleAuth.getMerchantToken(appKey, appSecret, (Map)null, url);
    }

    public String getMerchantToken(String appKey, String appSecret, Map<String, String> header, String... url) {
        this.verify(appKey, "AppKey不能为空");
        this.verify(appSecret, "AppSecret不能为空");
        return ModuleAuth.getMerchantToken(appKey, appSecret, header, url);
    }

    public String getISVToken(String appKey, String appSecret, String code, String taxnum, String redirectUri, String... url) {
        this.verify(code, "code不能为空");
        this.verify(taxnum, "taxnum不能为空");
        this.verify(appKey, "AppKey不能为空");
        this.verify(appSecret, "AppSecret不能为空");
        this.verify(redirectUri, "redirectUri不能为空");
        return ModuleAuth.getIsvToken(code, taxnum, appKey, appSecret, redirectUri, (Map)null, url);
    }

    public String getISVToken(String appKey, String appSecret, String code, String taxnum, String redirectUri, Map<String, String> header, String... url) {
        this.verify(code, "code不能为空");
        this.verify(taxnum, "taxnum不能为空");
        this.verify(appKey, "AppKey不能为空");
        this.verify(appSecret, "AppSecret不能为空");
        this.verify(redirectUri, "redirectUri不能为空");
        return ModuleAuth.getIsvToken(code, taxnum, appKey, appSecret, redirectUri, header, url);
    }

    public String refreshISVToken(String refreshToken, String userId, String appSecret, String... url) {
        this.verify(userId, "userId不能为空");
        this.verify(appSecret, "appSecret不能为空");
        this.verify(refreshToken, "refreshToken不能为空");
        return ModuleAuth.refreshIsvToken(refreshToken, userId, appSecret, (Map)null, url);
    }

    public String refreshISVToken(String refreshToken, String userId, String appSecret, Map<String, String> header, String... url) {
        this.verify(userId, "userId不能为空");
        this.verify(appSecret, "appSecret不能为空");
        this.verify(refreshToken, "refreshToken不能为空");
        return ModuleAuth.refreshIsvToken(refreshToken, userId, appSecret, header, url);
    }

    public NResponse sendPostSyncRequest(String url, String senid, String appKey, String appSecret, String token, String taxnum, String method, String content, int... timeout) {
        this.verify(senid, "senid不能为空");
        this.verify(appKey, "appKey不能为空");
        this.verify(method, "method不能为空");
        this.verify(url, "请求地址URL不能为空");
        this.verify(content, "content不能为空");
        this.verify(appSecret, "appSecret不能为空");

        try {
            long timestamp = System.currentTimeMillis() / 1000L;
            String nonce = String.valueOf(ThreadLocalRandom.current().nextInt(10000, 1000000000));
            StringBuffer sb = new StringBuffer(url);
            sb.append("?senid=").append(senid).append("&nonce=").append(nonce).append("&timestamp=").append(timestamp).append("&appkey=").append(appKey);
            Map<String, String> header = this.buildHeader(url, senid, appKey, appSecret, token, taxnum, method, content, nonce, String.valueOf(timestamp), (Map)null);
            String body = ModuleHttp.sendSyncHttp(sb.toString(), header, content, timeout);
            return new NResponse(sb.toString(), header, content, body);
        } catch (IOException var15) {
            throw new NException("发送HTTP请求异常", var15);
        }
    }

    public String sendPostSyncRequest(String url, String senid, String appKey, String appSecret, String token, String taxnum, String method, String content, Map<String, String> header, int... timeout) {
        this.verify(senid, "senid不能为空");
        this.verify(appKey, "appKey不能为空");
        this.verify(method, "method不能为空");
        this.verify(url, "请求地址URL不能为空");
        this.verify(content, "content不能为空");
        this.verify(appSecret, "appSecret不能为空");

        try {
            long timestamp = System.currentTimeMillis() / 1000L;
            String nonce = String.valueOf(ThreadLocalRandom.current().nextInt(10000, 1000000000));
            StringBuffer sb = new StringBuffer(url);
            sb.append("?senid=").append(senid).append("&nonce=").append(nonce).append("&timestamp=").append(timestamp).append("&appkey=").append(appKey);
            header = this.buildHeader(url, senid, appKey, appSecret, token, taxnum, method, content, nonce, String.valueOf(timestamp), header);
            return ModuleHttp.sendSyncHttp(sb.toString(), header, content, timeout);
        } catch (IOException var15) {
            throw new NException("发送HTTP请求异常", var15);
        }
    }

    public String convertToUtf8(String gbk) {
        try {
            byte[] gbkArr = gbk.getBytes("gbk");
            byte[] utf8Arr = (new String(gbkArr, "gbk")).getBytes("utf-8");
            return new String(utf8Arr, "utf-8");
        } catch (Exception var4) {
            throw new NException("字符编码转换异常", var4);
        }
    }

    private void verify(String v, String msg) {
        if (v == null || v.trim().length() == 0 || v.toLowerCase().trim().equals("null")) {
            throw new NException(msg);
        }
    }

    private Map<String, String> buildHeader(String url, String senid, String appKey, String appSecret, String token, String taxnum, String method, String content, String nonce, String timestamp, Map<String, String> header) throws MalformedURLException {
        URL httpUrl = new URL(url);
        String path = httpUrl.getPath();
        if (header == null || ((Map)header).isEmpty()) {
            header = new HashMap();
        }

        ((Map)header).put("Content-Type", "application/json");
        ((Map)header).put("X-Nuonuo-Sign", ModuleSign.getSign(path, appSecret, appKey, senid, nonce, content, timestamp));
        ((Map)header).put("accessToken", token);
        ((Map)header).put("userTax", taxnum);
        ((Map)header).put("method", method);
        ((Map)header).put("sdkVer", "1.0.5.2");
        return (Map)header;
    }
}
